package com.ptu.server.Impl;

import com.ptu.dto.UserDTO;
import com.ptu.entity.Favorite;
import com.ptu.mapper.FavoriteMapper;
import com.ptu.mapper.ProductMapper;
import com.ptu.server.FavoriteServer;
import com.ptu.util.UserHolder;
import com.ptu.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class FavoriteServerImpl implements FavoriteServer {

    @Autowired
    FavoriteMapper favoriteMapper;

    @Autowired
    ProductMapper productMapper;

    @Override
    public void add(Integer productId) {
        //获取用户id
        UserDTO user = UserHolder.getUser();
        int userId = user.getId();
        //创建时间，更新时间

        favoriteMapper.add(userId, productId);
    }

    @Override
    public void delete(Integer productId) {
        //获取用户id
        UserDTO user = UserHolder.getUser();
        int userId = user.getId();

        favoriteMapper.delete(userId, productId);
    }

    @Override
    public List<ProductVO> getFavoriteList() {
        //获取用户id
        UserDTO user = UserHolder.getUser();
        int userId = user.getId();
        System.out.println("用户idcslb: " + userId);

        List<Integer> productIds = favoriteMapper.getFavoriteList(userId);

        List<ProductVO> productVOS = new ArrayList<>();
        for (Integer productId : productIds) {
            ProductVO productVO = productMapper.getDetailById(productId);
            productVOS.add(productVO);
        }
        return productVOS;
    }

    @Override
    public boolean isFavorite(Integer productId) {
        //获取用户id
        UserDTO user = UserHolder.getUser();
        int userId = user.getId();
        System.out.println("用户id: " + userId + "是否收藏商品id: " + productId);
        if (favoriteMapper.getFavoriteList(userId).contains(productId)) {
            return true;
        } else {
            return false;
        }

    }
}
